/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-30 22:46:52
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-10 15:18:34
 * @LastEditors: judu233
 */
import { DefStringProtocol, NetData } from "../NetInterface";
import { NetManager } from "../NetManager";
import { NetNode } from "../NetNode";
import { WebSock } from "../WebSock";
import { netConfig } from "./NetConfig";
import { NetTips } from "./NetTips";
import { CallbackObject, IRequestProtocol } from "../NetInterface";

export enum NetChannelType {
    /** 游戏服务器 */
    Game = 0,
    /** 聊天服务器 */
    Chat = 1
}

class GameProtocol extends DefStringProtocol {
    /** 心跳协议 */
    getHearbeat(): NetData {
        return `{"action":"LoginAction","method":"heart","data":"null","isCompress":false,"channelid":${netConfig.channelid},"callback":"LoginAction_heart"}`;
    }
}
export class NetNodeGame extends NetNode {
    private isCompress: boolean = false;

    public req(action: string, method: string, data: any, rspObject: CallbackObject, showTips: boolean = true, force: boolean = false) {
        let protocol: IRequestProtocol = {
            action: action,
            method: method,
            data: JSON.stringify(data),
            isCompress: this.isCompress,
            channelid: netConfig.channelid
        }
        return this.request(protocol, rspObject, showTips, force);
    }

    public reqUnique(action: string, method: string, data: any, rspObject: CallbackObject, showTips: boolean = true, force: boolean = false): boolean {
        let protocol: IRequestProtocol = {
            action: action,
            method: method,
            data: JSON.stringify(data),
            isCompress: this.isCompress,
            channelid: netConfig.channelid
        }
        return super.requestUnique(protocol, rspObject, showTips, force);
    }
}

export class NetChannelManager {
    public game!: NetNodeGame;
    public chat!: NetNodeGame;

    /** 创建游戏服务器 */
    gameCreate() {
        this.game = new NetNodeGame();
        this.game.init(new WebSock(), new GameProtocol(), new NetTips());
        NetManager.getInstance().setNetNode(this.game, NetChannelType.Game);
    }

    /** 连接游戏服务器 */
    gameConnect() {
        NetManager.getInstance().connect({ url: `ws://${netConfig.lastLoginServer.host}:${netConfig.lastLoginServer.port}`, autoReconnect: 0 }, NetChannelType.Game);
    }

    /** 断开游戏服务器 */
    gameClose() {
        NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
    }

    /** 创建聊天服务器 */
    chatCreate() {
        this.chat = new NetNodeGame();
        this.chat.init(new WebSock(), new DefStringProtocol());
        NetManager.getInstance().setNetNode(this.chat, NetChannelType.Chat);
    }

    /** 连接聊天服务器 */
    chatConnect() {
        NetManager.getInstance().connect({ url: `ws://${netConfig.lastLoginServer.host}:${netConfig.lastLoginServer.chat_port}`, autoReconnect: 0 }, NetChannelType.Chat);
    }

    /** 断开游戏服务器 */
    chatClose() {
        NetManager.getInstance().close(undefined, undefined, NetChannelType.Chat);
    }
}

export var netChannel = new NetChannelManager();

